package cn.fldong.controller;

import cn.fldong.model.ClassInfo;
import cn.fldong.service.ClassService;
import cn.fldong.service.impl.ClassServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/classes")
public class ClassServlet extends HttpServlet {
    private ClassService classService;

    @Override
    public void init() throws ServletException {
        classService = new ClassServiceImpl();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        String action = request.getParameter("action");
        if (action == null) action = "list";

        try {
            switch (action) {
                case "list":
                    listClasses(request, response);
                    break;
                case "addPage":
                    request.getRequestDispatcher("/WEB-INF/views/class/add.jsp").forward(request, response);
                    break;
                case "add":
                    addClass(request, response);
                    break;
                case "editPage":
                    editPage(request, response);
                    break;
                case "update":
                    updateClass(request, response);
                    break;
                case "delete":
                    deleteClass(request, response);
                    break;
                default:
                    listClasses(request, response);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            request.setAttribute("errorMsg", "系统错误：" + e.getMessage());
            request.getRequestDispatcher("/WEB-INF/views/error.jsp").forward(request, response);
        }
    }

    private void listClasses(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        List<ClassInfo> classes = classService.getAllClasses();
        request.setAttribute("classes", classes);
        request.getRequestDispatcher("/WEB-INF/views/class/list.jsp").forward(request, response);
    }

    private void addClass(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        String classId = request.getParameter("classId");
        String className = request.getParameter("className");
        String grade = request.getParameter("grade");
        String major = request.getParameter("major");

        if (classService.classNameExists(className)) {
            request.setAttribute("errorMsg", "班级名称已存在");
            request.setAttribute("class", new ClassInfo(classId, className, grade, major));
            request.getRequestDispatcher("/WEB-INF/views/class/add.jsp").forward(request, response);
            return;
        }

        ClassInfo classInfo = new ClassInfo();
        classInfo.setClassId(classId);
        classInfo.setClassName(className);
        classInfo.setGrade(grade);
        classInfo.setMajor(major);

        boolean success = classService.addClass(classInfo);
        if (success) {
            request.setAttribute("successMsg", "班级添加成功");
            listClasses(request, response);
        } else {
            request.setAttribute("errorMsg", "班级添加失败");
            request.setAttribute("class", classInfo);
            request.getRequestDispatcher("/WEB-INF/views/class/add.jsp").forward(request, response);
        }
    }

    private void editPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        String classId = request.getParameter("classId");
        ClassInfo classInfo = classService.getClassById(classId);
        request.setAttribute("class", classInfo);
        request.getRequestDispatcher("/WEB-INF/views/class/edit.jsp").forward(request, response);
    }

    private void updateClass(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        String classId = request.getParameter("classId");
        String className = request.getParameter("className");
        String grade = request.getParameter("grade");
        String major = request.getParameter("major");

        ClassInfo existingClass = classService.getClassById(classId);
        if (!existingClass.getClassName().equals(className) && classService.classNameExists(className)) {
            request.setAttribute("errorMsg", "班级名称已存在");
            request.setAttribute("class", new ClassInfo(classId, className, grade, major));
            request.getRequestDispatcher("/WEB-INF/views/class/edit.jsp").forward(request, response);
            return;
        }

        ClassInfo classInfo = new ClassInfo();
        classInfo.setClassId(classId);
        classInfo.setClassName(className);
        classInfo.setGrade(grade);
        classInfo.setMajor(major);

        boolean success = classService.updateClass(classInfo);
        if (success) {
            request.setAttribute("successMsg", "班级更新成功");
            listClasses(request, response);
        } else {
            request.setAttribute("errorMsg", "班级更新失败");
            request.setAttribute("class", classInfo);
            request.getRequestDispatcher("/WEB-INF/views/class/edit.jsp").forward(request, response);
        }
    }

    private void deleteClass(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        String classId = request.getParameter("classId");
        boolean success = classService.deleteClass(classId);

        if (success) {
            request.setAttribute("successMsg", "班级删除成功");
        } else {
            request.setAttribute("errorMsg", "班级删除失败");
        }

        listClasses(request, response);
    }
}